<?php
if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
/**
 * short message service
 *
 * @author fan
 *
 */
class Sms {
	public function __construct() {

		$this->CI = & get_instance ();
	}
	
	/**
	 * [发送短信]
	 * @author fans
	 * @date 2016-7-20
	 **/
	public function send( $mobile , $content )
	{
		$code = time().rand(1000,9999);
		$content = UrlEncode(base64_encode(iconv("UTF-8","GBK",$content)));
		$url =SEND_URL.'?userId='.SEND_NAME.'&password='.SEND_PASS.'&multixmt='.$code.'|*|'.$mobile.'|'.$content;
		$result = sendHttp($url,'','get');
		return $this->errcode($result['str']);
	}

	/**
	 * [错误提示码]
	 * @param  [type] $code [description]
	 */
	public function errcode($code){
		$arr = array('-1','-12','-999','-2','-10','-11','-13','-14','-101','-102','-103','-200','-999','-10001','-10003','-10011','-10029','-10030','-10031','-10057','-10056');
		if(in_array($code,$arr)){
			return $code;
		}
		return 1;
	}

	/**
	 * 群发
	 * @param  [type] $mobile  [description]
	 * @param  [type] $content [description]
	 * @param  [type] $mc      [description]
	 * @return [type]          [description]
	 */
	public function mass($mobile,$content,$mc){
		$code = time().rand(1000,9999);

		$url = SEND_MASS_URL.'?userId='.SEND_NAME.'&password='.SEND_PASS.'&pszMobis='.$mobile.'&pszMsg='.$content.'&iMobiCount='.$mc.'&pszSubPort='.SEND_SUBPORT.'&MsgId='.$code;
		
		$result = sendHttp($url,'','get');

		$code = json_decode(json_encode(simplexml_load_string($result['str'], 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		$flag = $this->errcode($code[0]);

		$this->CI->load->library('logs');
		$this->CI->logs->smsmass($url.' 状态码：'.$flag."\n \r");//记录日志;

		return $flag;

	}


	//获取短信接口余额
	public function balance(){
		$url ='http://61.145.229.28:8027/MWGate/wmgw.asmx/MongateQueryBalance?userId='.SEND_NAME.'&password='.SEND_PASS;
		$result = sendHttp($url,'','get');
		$code = json_decode(json_encode(simplexml_load_string($result['str'], 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $code[0];
	}

}